import java.net.*;
import java.io.*;

public class URLConnectionReader {

	public static void write_file() throws Exception {
		try{
			// Create file 
			FileWriter file_writer = new FileWriter("SPAM.XML");
			BufferedWriter file_buffered_writer = new BufferedWriter(file_writer);
			file_buffered_writer.write("Hello World...");
			//Close the output stream
			file_buffered_writer.close();
		} catch (Exception e){
			//Ugly Catch exception...
			System.err.println("Error: " + e.getMessage());
		}
	}

	public static void main(String[] args) throws Exception {
		try {
			//export http_proxy="http://FIATAUTO\U070775:******@151.92.***.***:8080/"
			System.getProperties().setProperty( "http.proxyHost", "151.92.175.61" );
			System.getProperties().setProperty( "http.proxyPort", "8080" );
			// Create file 
			FileWriter file_writer = new FileWriter("SPAM.XML");
			BufferedWriter file_buffered_writer = new BufferedWriter(file_writer);
			// Create the URL Connection
			URL fdt = new URL("http://opendata.5t.torino.it/get_fdt");
			URLConnection url_connection = fdt.openConnection();
			BufferedReader in = new BufferedReader(
									new InputStreamReader(
									url_connection.getInputStream()));
			String input_line;
			int num_lines = 0;
			while ((input_line = in.readLine()) != null) {
				//System.out.println(input_line);
				file_buffered_writer.write(input_line + "\n");
				num_lines ++;
			}
			System.out.println("Writed " + num_lines + " lines.");
			in.close();
		} catch (Exception e){
			//Ugly Catch exception...
			System.err.println("Error: " + e.getMessage());
		}
	}
}

/**
* reading from a URLConnection instead of reading directly 
* from a URL might be more useful. This is because you can use 
* the URLConnection object for other tasks (like writing to the URL) 
* at the same time.
*/